package com.chen.gcode.fx.controllers;

import com.chen.gcode.utils.GCodeGenerate;
import com.chen.gcode.utils.Setting;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.input.DragEvent;
import javafx.scene.layout.AnchorPane;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

public class VPNLoginContorller implements Initializable,PingCallBack,ConnectCallBack {

    @FXML
    private AnchorPane pane;

    @FXML
    private TextField testip;

    @FXML
    private TextField pptpuser;

    @FXML
    private TextField pptppwd;

    @FXML
    private TextField netName;

    @FXML
    private Button button;

    @FXML
    private ProgressIndicator loading;

    @FXML
    private  TextArea logarea;


    private String log = "";

    private static Timer timer=new Timer();

    private boolean isStartTest = false;

    private Integer lostConnectTime = 0;


    public boolean isStartTest() {
        return isStartTest;
    }

    public void setStartTest(boolean startTest) {
        isStartTest = startTest;
        if (isStartTest){


            loading.setVisible(true);
        }else {

            loading.setVisible(false);
        }
    }

    public void testConnectStart(){
        button.setText("停止检测");
        System.out.println("开始检测VPN在线状态");
        logarea.setText("开始检测VPN在线状态");
        if (this.isStartTest){
            return;
        }

        setStartTest(true);

        timer=new Timer();
        PingCallBack callBack = this;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                PingCmdTask pingCmdTask = new PingCmdTask("cmd.exe /C ping -n 1 -w 1000 "+testip.getText(),callBack);
                new Thread(pingCmdTask).start();
            }
        },0,3000);
    }

    public void testConnectStop(){
        button.setText("开始检测");
        System.out.println("停止检测VPN在线状态");
        logarea.setText("停止检测VPN在线状态");
        if (!this.isStartTest){
            return;
        }
        setStartTest(false);
        timer.cancel();
    }

    public void restartNet(){

        try {
            ConnectCmdTask connectCmdTask = new ConnectCmdTask("cmd.exe /C rasdial \""+netName.getText()+"\" \""+pptpuser.getText()+"\" \""+pptppwd.getText()+"\"",this);
            new Thread(connectCmdTask).start();
        }catch (Exception e){

        }


    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }


    @FXML
    void getFile(DragEvent event) {
        List<File> files = event.getDragboard().getFiles();
        //获得文件
        File file = files.get(0);
        //之后的相关操作，获得文件路径等..
        if (file != null){

        }
    }

    @FXML
    public void onclick(ActionEvent e){
        if (this.isStartTest == false){
            testConnectStart();
        }else {
            testConnectStop();
        }
//        CmdTask cmdTask = new CmdTask("rasdial \"VPN 连接\" 'ss' 'aa'");
//        CmdTask cmdTask = new CmdTask("cmd.exe /C rasdial \"VPN 连接\" \"ss\" \"aa\"");

//        DirectoryChooser directoryChooser=new DirectoryChooser();
//        File file = directoryChooser.showDialog(pane.getScene().getWindow());
//        String path = file.getPath();//选择的文件夹路径
//
//        if (path != null){
//
//        }

    }

    @FXML
    public void save(ActionEvent e){



    }

    public void openDir(){
        File file = new File(Setting.getInstance().getSavePath().trim());
        try {
            if (file != null && file.exists())
                Desktop.getDesktop().open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void makeTest(){
        GCodeGenerate gCodeGenerate = new GCodeGenerate();
        gCodeGenerate.testCode(Setting.getInstance().getSavePath()+File.separator+"test.nc");
        openDir();
    }


    @Override
    public void connect() {
        System.out.println("链接正常");
        logarea.setText("连接正常");
        lostConnectTime = 0;
    }

    @Override
    public void lostConnect() {
        System.out.println("链接丢失");
        logarea.setText("连接异常");
        lostConnectTime++;

        if (lostConnectTime > 5){
            lostConnectTime = 0;
            testConnectStop();
            System.out.println("重连网卡");
            logarea.setText("重连网卡");
            restartNet();
        }
    }

    @Override
    public void connectSuccess() {
        System.out.println("连接vpn成功");
        logarea.setText("连接vpn成功");
        testConnectStart();
    }

    @Override
    public void connectFail() {
        System.out.println("连接vpn失败");
        logarea.setText("连接vpn失败");
        testConnectStart();
    }
}
